<?php
namespace addons\paotui\service;

use app\common\model\User;
use fast\Random;
use think\Db;
use think\exception\PDOException;

/**
 *
 */
class Third
{
    public static $error;

    /**
     * 用户端认证
     * @param $type
     * @param array $params
     * @return bool
     * @throws \think\exception\DbException
     */
    public static function connect($type, $params = [])
    {
        $data = [
            'type' => $type,
            'openid' => $params['openid'],
            'nickname' => isset($params['nickname']) ? $params['nickname'] : '',
        ];
        $auth = \app\common\library\Auth::instance();
        $wechatUser = \app\admin\model\paotui\Third::get(['type'=>$type,'openid' => $params['openid']]);
        if ($wechatUser) {

            $user = User::get($wechatUser['user_id']);
            if (!$user) {
                // 先随机一个用户名,随后再变更为u+数字id
                $username = Random::alnum(20);
                $password = Random::alnum(6);
                $domain = request()->host();

                Db::startTrans();
                try {
                    $result = $auth->register($username, $password, $username . '@' . $domain, '',['nickname'=>$data['nickname']]);
                    if (!$result) {

                        self::$error = $auth->getError();
                        return false;
                    }
                    $user = $auth->getUser();
                    $fields = [
                        'username' => 'wx' . $user->id,
                        'email' => 'wx' . $user->id . '@' . $domain,
                        'nickname'=>$data['nickname'],
                    ];

                    // 更新会员资料
                    $user = User::get($user->id);
                    $user->save($fields);

                    $data['user_id'] = $user->id;
                    $wechatUser->save($data);

                    \addons\paotui\service\User::bindDefaultLevel($user->id);
                    Db::commit();
                } catch (PDOException $e) {
                    Db::rollback();

                    self::$error = $e->getMessage();
                    return false;
                }
            }
        } else {
            // 先随机一个用户名,随后再变更为u+数字id
            $username = Random::alnum(20);
            $password = Random::alnum(6);
            $domain = request()->host();

            Db::startTrans();
            try {
                $result = $auth->register($username, $password, $username . '@' . $domain, '',['nickname'=>$data['nickname']]);
                if (!$result) {
                    return false;
                }
                $user = $auth->getUser();
                $fields = [
                    'username' => 'wx' . $user->id,
                    'email' => 'wx' . $user->id . '@' . $domain,
                    'nickname'=>$data['nickname']
                ];

                // 更新会员资料
                $user = User::get($user->id);
                $user->save($fields);

                $data['user_id'] = $user->id;
                \app\admin\model\paotui\Third::create($data);
                \addons\paotui\service\User::bindDefaultLevel($user->id);
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                self::$error = $e->getMessage();
                return false;
            }
        }
        return $auth->direct($user->id);
    }
}